﻿using System;
using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;

namespace Platform.DB
{
    public sealed class DbAccess
    {
        public static bool GreatDB(string name)
        {
            return DbFactory.GetInstance().CreateDb(name);
        }

        public static int BatExecuteNonQuery(List<string> commandTextList)
        {
            int result = 0;
            IDbConnection dbConnection = DbFactory.GetInstance().CreateConnection();
            IDbTransaction dbTransaction = null;
            try
            {
                dbConnection.Open();
                dbTransaction = dbConnection.BeginTransaction();
                foreach (string commandText in commandTextList)
                {
                    IDbCommand dbCommand = dbConnection.CreateCommand();
                    dbCommand.CommandText = commandText;
                    dbCommand.Transaction = dbTransaction;
                    result = dbCommand.ExecuteNonQuery();
                }
                dbTransaction.Commit();
            }
            catch
            {
                if (dbTransaction != null)
                {
                    dbTransaction.Rollback();
                }
            }
            finally
            {
                try
                {
                    dbConnection.Close();
                }
                catch
                {
                }
            }
            return result;
        }

        public static int ExecuteSql(string commandText)
        {
            int result = 0;
            IDbConnection dbConnection = DbFactory.GetInstance().CreateConnection();
            try
            {
                dbConnection.Open();
                IDbCommand dbCommand = dbConnection.CreateCommand();
                dbCommand.CommandText = commandText;
                result = dbCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                try
                {
                    dbConnection.Close();
                }
                catch
                {
                }
            }
            return result;
        }

        public static int ExecuteSql(string commandText, string[] paramNames, object[] paramValues)
        {
            int result = 0;
            IDbConnection dbConnection = DbFactory.GetInstance().CreateConnection();
            try
            {
                dbConnection.Open();
                IDbCommand dbCommand = dbConnection.CreateCommand();
                dbCommand.CommandText = commandText;
                for (int i = 0; i < paramNames.Length; i++)
                {
                    IDataParameter dataParameter = DbFactory.GetInstance().CreateDataParameter();
                    dataParameter.ParameterName = paramNames[i];
                    dataParameter.Value = paramValues[i];
                    dbCommand.Parameters.Add(dataParameter);
                }
                result = dbCommand.ExecuteNonQuery();
            }
            finally
            {
                try
                {
                    dbConnection.Close();
                }
                catch
                {
                }
            }
            return result;
        }

        public static int ExecuteSql(string commandText, IDataParameter[] parameters)
        {
            int result = 0;
            IDbConnection dbConnection = DbFactory.GetInstance().CreateConnection();
            try
            {
                dbConnection.Open();
                IDbCommand dbCommand = dbConnection.CreateCommand();
                dbCommand.CommandText = commandText;
                for (int i = 0; i < parameters.Length; i++)
                {
                    dbCommand.Parameters.Add(parameters[i]);
                }
                result = dbCommand.ExecuteNonQuery();
            }
            finally
            {
                try
                {
                    dbConnection.Close();
                }
                catch
                {
                }
            }
            return result;
        }

        public static int ExecuteSql(IDbTransaction tran, string commandText)
        {
            int result = 0;
            IDbConnection dbConnection = DbFactory.GetInstance().CreateConnection();
            try
            {
                dbConnection.Open();
                IDbCommand dbCommand = dbConnection.CreateCommand();
                if (tran != null)
                {
                    dbCommand.Transaction = tran;
                }
                dbCommand.CommandText = commandText;
                result = dbCommand.ExecuteNonQuery();
            }
            finally
            {
                if (dbConnection != null)
                {
                    dbConnection.Close();
                }
            }
            return result;
        }

        public static int ExecuteSql(IDbTransaction tran, string commandText, IDataParameter[] cmdParms)
        {
            int result = 0;
            IDbConnection dbConnection = DbFactory.GetInstance().CreateConnection();
            try
            {
                dbConnection.Open();
                IDbCommand dbCommand = dbConnection.CreateCommand();
                if (tran != null)
                {
                    dbCommand.Transaction = tran;
                }
                dbCommand.CommandText = commandText;
                foreach (IDataParameter value in cmdParms)
                {
                    dbCommand.Parameters.Add(value);
                }
                result = dbCommand.ExecuteNonQuery();
            }
            finally
            {
                if (dbConnection != null)
                {
                    dbConnection.Close();
                }
            }
            return result;
        }

        public static DataTable ExecuteQuery(string commandText)
        {
            DataSet dataSet = new DataSet();
            IDbConnection dbConnection = DbFactory.GetInstance().CreateConnection();
            DataTable result;
            try
            {
                dbConnection.Open();
                IDbCommand dbCommand = dbConnection.CreateCommand();
                dbCommand.CommandText = commandText;
                IDbDataAdapter dbDataAdapter = DbFactory.GetInstance().CreateDataAdapter();
                dbDataAdapter.SelectCommand = dbCommand;
                dbDataAdapter.Fill(dataSet);
                result = dataSet.Tables[0];
            }
            catch
            {
                result = null;
            }
            finally
            {
                try
                {
                    dbConnection.Close();
                }
                catch
                {
                }
            }
            return result;
        }

        /// <summary>
        /// 根据表名进行数据检索
        /// </summary>
        /// <param name="commandText">命令文本</param>
        /// <param name="dbname">主表名称</param>
        /// <returns></returns>
        public static DataTable ExecuteQuery(string commandText,string dbname)
        {
            DataSet dataSet = new DataSet();
            IDbConnection dbConnection = DbFactory.GetInstance().CreateConnection1(dbname);
            DataTable result;
            try
            {
                dbConnection.Open();
                IDbCommand dbCommand = dbConnection.CreateCommand();
                dbCommand.CommandText = commandText;
                IDbDataAdapter dbDataAdapter = DbFactory.GetInstance().CreateDataAdapter();
                dbDataAdapter.SelectCommand = dbCommand;
                dbDataAdapter.Fill(dataSet);
                result = dataSet.Tables[0];
            }
            catch
            {
                result = null;
            }
            finally
            {
                try
                {
                    dbConnection.Close();
                }
                catch
                {
                }
            }
            return result;
        }

        public static IDataReader ExecuteQueryReturnDataReader(string commandText)
        {
            IDataReader dataReader = null;
            IDbConnection dbConnection = DbFactory.GetInstance().CreateConnection();
            IDataReader result;
            try
            {
                dbConnection.Open();
                IDbCommand dbCommand = dbConnection.CreateCommand();
                dbCommand.CommandText = commandText;
                dataReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection);
                result = dataReader;
            }
            catch
            {
                try
                {
                    if (dataReader != null)
                    {
                        dataReader.Close();
                    }
                    else
                    {
                        dbConnection.Close();
                    }
                }
                catch
                {
                }
                result = null;
            }
            return result;
        }

        public static IDataReader ExecuteQueryReturnDataReader(string commandText, string[] paramNames, object[] paramValues)
        {
            IDataReader dataReader = null;
            IDbConnection dbConnection = DbFactory.GetInstance().CreateConnection();
            try
            {
                dbConnection.Open();
                IDbCommand dbCommand = dbConnection.CreateCommand();
                dbCommand.CommandText = commandText;
                for (int i = 0; i < paramNames.Length; i++)
                {
                    IDataParameter dataParameter = DbFactory.GetInstance().CreateDataParameter();
                    dataParameter.ParameterName = paramNames[i];
                    dataParameter.Value = paramValues[i];
                    dbCommand.Parameters.Add(dataParameter);
                }
                dataReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch
            {
                try
                {
                    if (dataReader != null)
                    {
                        dataReader.Close();
                    }
                    else
                    {
                        dbConnection.Close();
                    }
                }
                catch
                {
                }
                return null;
            }
            return dataReader;
        }

        public static float[] ExecuteQueryReturnFloatValues(string strSql)
        {
            IDataReader dataReader = DbAccess.ExecuteQueryReturnDataReader(strSql);
            if (dataReader == null)
            {
                return null;
            }
            try
            {
                if (dataReader.Read())
                {
                    float[] array = new float[dataReader.FieldCount];
                    for (int i = 0; i < dataReader.FieldCount; i++)
                    {
                        try
                        {
                            array[i] = dataReader.GetFloat(i);
                        }
                        catch
                        {
                            array[i] = 0f;
                        }
                    }
                    return array;
                }
            }
            catch
            {
                return null;
            }
            finally
            {
                dataReader.Close();
                dataReader = null;
            }
            return null;
        }

        public static int QueryRecordCount(string tableName, string where)
        {
            string commandText = string.Format("SELECT COUNT(*) FROM {0}{1}", tableName, string.IsNullOrEmpty(where) ? "" : (" WHERE " + where));
            DataTable dataTable = DbAccess.ExecuteQuery(commandText);
            int result;
            try
            {
                if (dataTable == null)
                {
                    result = 0;
                }
                else
                {
                    result = Convert.ToInt32(dataTable.Rows[0][0].ToString());
                }
            }
            catch
            {
                result = 0;
            }
            return result;
        }

        public static int QueryRecordCount(string strSql)
        {
            DataTable dataTable = DbAccess.ExecuteQuery(strSql);
            return Convert.ToInt32(dataTable.Rows[0][0].ToString());
        }

        public static bool HaveData(string tableName, string beginTime, string endTime)
        {
            string commandText = string.Concat(new string[]
            {
                "select count(*) from ",
                tableName,
                " where datatime>='",
                beginTime,
                "' and datatime<='",
                endTime,
                "'"
            });
            IDataReader dataReader = DbAccess.ExecuteQueryReturnDataReader(commandText);
            if (dataReader == null)
            {
                return false;
            }
            try
            {
                if (dataReader.Read())
                {
                    int @int = dataReader.GetInt32(0);
                    return @int != 0;
                }
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                dataReader.Close();
                dataReader = null;
            }
            return false;
        }

        public static string[] GetDbInfos(string strSql)
        {
            List<string> list = new List<string>();
            IDataReader dataReader = DbAccess.ExecuteQueryReturnDataReader(strSql);
            if (dataReader == null)
            {
                return null;
            }
            string[] result;
            try
            {
                while (dataReader.Read())
                {
                    string @string = dataReader.GetString(0);
                    list.Add(@string);
                }
                result = list.ToArray();
            }
            finally
            {
                dataReader.Close();
            }
            return result;
        }

    }
}
